<?php
include("./includes/common.php");
@header("content-Type:text/json;charset=utf-8");
if(isset($_GET['act'])) {
    $act = $_GET['act'];
} else {
    $act = $_POST['act'];
}
switch($act) {
   case 'Upload'://加密上传文件
   
   if (!is_dir('File')) mkdir('File',0755);
   if (!is_dir(PACKAGE_DIR)) mkdir(PACKAGE_DIR,0755);
   if (!is_dir(ENCODE)) mkdir(ENCODE,0755);

    if(fileName($_FILES['file']['name']) != 'zip') {
        $arr = array('code'=>'-1', 'msg'=>'请上传ZIP压缩包文件');
        die(json_encode($arr));
    }
    if (preg_match("/[\x7f-\xff]/", $_FILES['file']['name'])) {
        $arr = array('code'=>'-1', 'msg'=>'文件名称不得带有中文字符');
        die(json_encode($arr));
    }
    if(($_FILES["file"]["size"]/1024) > 2048) {
        $arr = array('code'=>'-1', 'msg'=>'上传文件过大');
        die(json_encode($arr));
    }
    $app = PACKAGE_DIR;
    //获取当前IP作为文件目录
    $ip=real_ip();
    //获取上传文件名
    $file = $_FILES['file']['name'];
    //获取当前时间
    $time = date("Y-m-d H:i:s");
    //定义缓存目录名

    $space = $ip;
    //创建缓存目录
    if (!is_dir($app . $space . '/')) mkdir($app . $space . '/');
    $PathFile = $app . $space . '/' . $file;
    // var_dump($PathFile);
    // exit;
    //将上传文件保存到缓存目录
    copy($_FILES['file']['tmp_name'], $PathFile);
    // 判断文件夹内文件个数
    $count = getPathFileZip($PathFile);
    if($count >= 11) {
        $arr = array('code'=>'-1', 'msg'=>'压缩包内文件不得超过10个！');
        die(json_encode($arr));
    }
    $edition = daddslashes($_POST['edition']);
    $arr = array(
        'code'=>'1',
        'msg'=>'上传成功！',
        'data' => [
            'count'=>$count,
            'time'=>$time,
            'space'=>$space,
            'fileName'=>$_FILES['file']['name']
        ]
    );
    die(json_encode($arr));
    break;
    case 'Encode'://加密
    $edition = daddslashes($_POST['edition']);
    if($edition == 'PhPenc') {
        $edition = 'PhPenc';
    } else {
        $arr = array('code'=>'-1', 'msg'=>'PhPenc版本不存在！');
        die(json_encode($arr));
    }
    
    $file = daddslashes($_POST['file']);
    
    if(!empty($_POST['comment'])) {
        $comment = daddslashes($_POST['comment']);
    } else {
        $comment = 'Phantom Encryption授权加密系统由Phantomenc全权拥有 [ '.$edition.' ]';
    }
    
    $ip=real_ip();
    
    $pathFile = canchephp . $ip. '/' . $file;//上传存放目录
    
    $downs = ENCODE. $ip. '/';
    
    //创建加密缓存目录
    if (!is_dir(ENCODE . $ip . '/')) mkdir(ENCODE . $ip . '/');
    $down = ENCODE . $ip . '/' . $file;//加密文件存放目录
    
    copy($file, $down);//加密文件保存到缓存目录
    
    // var_dump($downs);
    // exit;
    
    
    $data = (new Encrypt($edition,$comment))->submit($pathFile, $down);
    
    $result = json_decode($data, true);
    
    $wenjian= md5(ENCODE .$ip);
    if($result['code'] == 1) {
        $arr = array(
            'code'=>'1',
            'msg'=>$result['info'],
            'data' => [
                'down' => $wenjian,
                'name' => $file
            ]
        );
        die(json_encode($arr));
    } else {
        $arr = array('code'=>'-1', 'msg'=>$result['info']);
        die(json_encode($arr));
    }
    break;
    case 'down'://判断文件
    if(isset($_GET['space']) && isset($_GET['file'])) {
        // 文件真实地址
        $ip=real_ip();
        $route = ENCODE . $ip . '/' . $_GET['file'];
        // var_dump($route);
        // exit;
        // 判断是否存在此文件
        if(!file_exists($route)) {
            // header('HTTP/1.1 404 Not Found');
            die('File does not exist！');
        }
        //告诉浏览器这是一个文件流格式的文件
        header("Content-type: application/octet-stream");
        header('Content-Disposition:attachment;filename=' . $_GET['file']);
        header('Content-Length:'.filesize($route));
        readfile($route);
    }else{
        $arr = array('code'=>'-1','msg'=>'主要参数不能为空！！');
        die(json_encode($arr));
    }
    break;
    default:
    $arr = array('code'=>'-4','msg'=>'No Act！');
    die(json_encode($arr));
    break;
}

function fileName($file) {
    $extension=explode('.',$file);
    if (($length = count($extension)) > 1) {
        $name = strtolower($extension[$length - 1]);
        return $name;
    }
    return false;
}
function getPathFileZip($zip_file, $extension = 'php') {
    $zip = new \ZipArchive();
    $zip->open($zip_file);
    $count = 0;
    for ($i = 0; $i < $zip->numFiles; $i++) {
        $stat = $zip->statIndex($i);
        $filename = $stat['name'];
        if (strtolower(substr($filename, -strlen($extension))) == $extension) {
            $count++;
        }
    }
    $zip->close();
    return $count;
}
?>